import Vue from 'vue';
let MinNoticeConstructor = Vue.extend(require('./main.vue'));

let instance;
let instances = [];
let seed = 1;
let nextZIndex=19900112;

var MinNotice = function(options) {
    if (Vue.prototype.$isServer) return;
    options = options || {};
    if (typeof options === 'string') {
        options = {
            message: options
        };
    }
    console.log(options)
    let id = 'minNotice_' + seed++;
    instance = new MinNoticeConstructor({
        data: options
    });
    instance.id = id;
    instance.vm = instance.$mount();
    document.body.appendChild(instance.vm.$el);
    instance.vm.visible = true;
    instance.dom = instance.vm.$el;
    instance.dom.style.zIndex =nextZIndex+seed;
    instances.push(instance);
    return instance.vm;
};

MinNotice.close = function(id, userOnClose) {
    for (let i = 0, len = instances.length; i < len; i++) {
        console.log(id === instances[i].id)
        if (id === instances[i].id) {
            if (typeof userOnClose === 'function') {
                userOnClose(instances[i]);
            }
            instances.splice(i, 1);
            break;
        }
    }
};

export default MinNotice;